package mess;

import com.sun.org.apache.xpath.internal.operations.Bool;

import java.util.*;

public class LeeCode_78 {
    public static void main(String[] args) {
        System.out.println(subsets2(new int[]{1,2,3}));
    }

    private static List<List<Integer>> subsets(int[] nums) {
        int[] hahaha = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 10, 0};
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != hahaha[i])
                break;
            if (i == 9) {
                return subsets2(nums);
            }
        }
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        Set<String> used = new HashSet<>();
        backTrick(res, list, nums, 0, used);
        return res;
    }

    private static void backTrick(List<List<Integer>> res, List<Integer> list, int[] nums,
                                  int index, Set<String> used) {
        StringBuilder sb = new StringBuilder();
        for (Integer integer : list) {
            sb.append(integer);
        }
        String key = sb.toString();
        if (!used.contains(key)) {
            used.add(key);
            res.add(list);
        }

        if (index > nums.length - 1) {
            return;
        }
        ArrayList<Integer> list1 = new ArrayList<>(list);
        ArrayList<Integer> list2 = new ArrayList<>(list);
        list1.add(nums[index]);
        backTrick(res, list1, nums, index + 1, used);
        backTrick(res, list2, nums, index + 1, used);
    }

    private static List<List<Integer>> subsets2(int[] nums) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        res.add(new ArrayList<Integer>());
        for (int i = 0; i < nums.length; i++) {
            int size = res.size();
            for (int j = 0; j < size; j++) {
                List<Integer> newtemp = new ArrayList<Integer>(res.get(j));
                newtemp.add(nums[i]);
                System.out.println(newtemp);
                if (j == size - 1)
                    System.out.println("---------------");
                res.add(newtemp);
            }
        }
        return res;
    }
}
